<template>
  <div class="table-body">
    <el-button size="small" type="primary" @click="showRuleFn(0)" style="margin-bottom: 10px">add new rule</el-button>
    <el-table :data="rulesList">
      <el-table-column label="ID" prop="id" width="80"/>
      <el-table-column label="Rule Name" prop="name"/>
      <el-table-column label="beginTime" prop="beginTime"/>
      <el-table-column label="endTime" prop="endTime"/>
<!--      <el-table-column label="Task Result" prop="roleSort" width="200">-->
<!--        <template slot-scope="scope">-->
<!--          <el-progress :percentage="scope.row.progress" :format="format" :color="scope.row.type"></el-progress>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="operate" prop="roleSort" width="110">-->
<!--        <template slot-scope="scope">-->
<!--          <el-link>Try again</el-link>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="operate" prop="roleSort" width="220" align="center">
        <template slot-scope="scope">
          <el-button size="mini" plain @click="copyLine(scope.row.id)">copy</el-button>
          <el-button type="primary" size="mini" plain @click="showRuleFn(scope.row.id)">edit</el-button>
          <el-button type="danger" size="mini" plain @click="handleDelete(scope.row)">delete</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRules, delRules } from "@/api/system/rules";
export default {
  name: "tabWarning",
  props:['detailId'],
  data() {
    return {
      rulesList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
        deviceId: 0
      },
    }
  },
  created() {
    this.queryParams.deviceId = this.detailId
    this.getList()
  },
  methods: {
    // format(percentage) {
    //   return percentage === 100 ? 'success' : `${percentage}%`;
    // },
    /** 查询规则列表 */
    getList() {
      this.loading = true;
      listRules(this.queryParams).then(response => {
        this.rulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    copyLine(rowId){
      this.$emit('showRuleJson',{type: 'copy', id: rowId})
    },
    showRuleFn(rowId) {
      this.$emit('showRuleJson',{type: 'edit', id: rowId})
    },
    /** delete按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认delete规则编号为"' + row.id + '"的数据项？').then(function() {
        return delRules(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("delete成功");
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>
.table-body {
  background: #fff;
  padding: 10px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .1);
  border: 1px solid #e6ebf5;
  border-radius: 5px;
}
</style>
